import { View } from '@tarojs/components';
import { Button } from '@nutui/nutui-react-taro';
import appConfig from '@/utils/config';
import './style.less';
import { wechatCode2SessionV1, getPhoneByCode, weChatPubCode2SessionV1 } from '@/service';
import Taro, { useRouter } from '@tarojs/taro';
import { getUserStore, useUserStore } from '@/common/hox/userStore';
import { useRequest } from 'ahooks';
const onClickPage = (props: { isCheck: boolean }) => {
    const { loginAsync, refreshUser, userInfo } = useUserStore();
    const { params } = useRouter();
    const navUrl = params.navUrl ? decodeURIComponent(params.navUrl) : null;
    const tabBarPages = [
        '/pages/index/index',
        '/pages/movie/index',
        '/pages/order/index',
        '/pages/my/index',
    ];
    const afterClose = async () => {
        // 如果配置跳转地址，则到相应地址
        if (navUrl && navUrl !== undefined) {
            if (tabBarPages.find((item) => navUrl.includes(item))) {
                Taro.switchTab({ url: navUrl });
            } else Taro.redirectTo({ url: navUrl });
            return;
        }

        if (process.env.TARO_ENV == 'weapp') {
            Taro.navigateBack({
                delta: 2,
            });
        } else {
            Taro.navigateBack();
        }
        // } catch (error) { }
    };
    const login = async () => {
        if (!props.isCheck) {
            Taro.showToast({
                title: '请先勾选并同意隐私协议以及注册协议',
                icon: 'none',
            });
            return;
        }
        if (process.env.TARO_ENV !== 'h5') {
            const { code } = await Taro.login();
            const { accessToken } = await wechatCode2SessionV1({ code });
            Taro.setStorageSync('token', accessToken);
            await loginAsync();
            await refreshUser();
            afterClose();
        }
    };
    const getPhone = useRequest(getPhoneByCode, {
        manual: true,
        onSuccess: async (res) => {
            getUserStore()?.setToken(res?.accessToken);
            await loginAsync();
            await refreshUser();
            afterClose();
        },
    });
    const getPhoneNumber = async (code: string) => {
        // 获取手机号绑定登录信息
        if (!props.isCheck) {
            Taro.showToast({
                title: '请先勾选并同意隐私协议以及注册协议',
                icon: 'none',
            });
            return;
        }
        getPhone.run(code);
    };

    return (
        <View className="onClick-page">
            {/* 未授权登录 */}
            {!userInfo?.userId && (
                <div
                    className="click-btn"
                    // openType={props.isCheck ? 'getPhoneNumber' : undefined}
                    onClick={() => login()}
                >
                    一键登录
                </div>
            )}
            {process.env.TARO_ENV !== 'h5' && userInfo && userInfo.mobile && (
                <Button
                    className="click-btn"
                    openType={props.isCheck ? 'getPhoneNumber' : undefined}
                    onGetPhoneNumber={(e) => e?.detail?.code && getPhoneNumber(e.detail.code)}
                >
                    一键获取手机号
                </Button>
            )}
        </View>
    );
};

export default onClickPage;
